package org.aplikator.server.descriptor;

import java.util.ArrayList;
import java.util.List;

import org.aplikator.client.descriptor.ApplicationDTO;
import org.aplikator.server.persistence.PersisterFactory;
import org.aplikator.server.util.Configurator;


public class Application {

    // singleton
    private static Application instance = instance();

    private static Application instance() {



        @SuppressWarnings("rawtypes")
        Class clazz;
        try {
            clazz = Class.forName(Configurator.get().getConfig().getString(Configurator.STRUCTURE));
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e);
        }
        try {
            return (Application) clazz.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalStateException(e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }

    public static Application get() {
        return instance;
    }


    private List<Menu> menus = new ArrayList<Menu>();



    protected Application() {
    }

    public Application addMenu(Menu menu) {
        menus.add(menu);
        return this;
    }


    public ApplicationDTO getApplicationDTO(org.aplikator.server.Context ctx){
        ApplicationDTO retval = new ApplicationDTO();
        for (Menu m: menus){
            retval.addMenu(m.getMenuDTO(ctx));
        }
        return retval;
    }




    public String generateDDL(boolean updateDB) {
        return PersisterFactory.getPersister().generateDDL(updateDB);

    }
}
